package com.qiaoba.module.job.controller;

import com.github.pagehelper.PageHelper;
import com.qiaoba.api.job.entity.dto.JobUpdateStatusDTO;
import com.qiaoba.api.job.entity.dto.SysJobDTO;
import com.qiaoba.api.job.entity.vo.SysJobVO;
import com.qiaoba.api.log.annotation.Log;
import com.qiaoba.api.log.enums.BusinessType;
import com.qiaoba.common.base.entity.Result;
import com.qiaoba.common.base.validate.AddGroup;
import com.qiaoba.common.base.validate.EditGroup;
import com.qiaoba.common.database.entity.PageQuery;
import com.qiaoba.common.database.entity.PageResult;
import com.qiaoba.module.job.entity.SysJob;
import com.qiaoba.module.job.entity.param.SysJobParam;
import com.qiaoba.module.job.service.SysJobService;
import com.qiaoba.poi.excel.util.ExcelUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.Size;
import java.util.List;

/**
 * 定时任务 Web层
 *
 * @author ailanyin
 * @date 2023-09-09 16:16:42
 */
@Validated
@RestController
@RequestMapping("/monitor/job")
@RequiredArgsConstructor
@Tag(name = "定时任务")
public class SysJobController {

    private final SysJobService sysJobService;

    @PreAuthorize("hasAuthority('monitor:job:list')")
    @GetMapping("/list")
    @Operation(summary = "获取列表")
    public PageResult<SysJob> list(@Validated SysJobParam param, PageQuery pageQuery) {
        PageHelper.startPage(pageQuery.getPageNum(), pageQuery.getPageSize());
        List<SysJob> list = sysJobService.selectList(param);
        return PageResult.build(list);
    }

    @PreAuthorize("hasAuthority('monitor:job:query')")
    @GetMapping(value = "/{jobId}")
    @Operation(summary = "获取详细")
    public Result<SysJobVO> getInfo(@Size(max = 20, message = "ID不合法") @PathVariable String jobId) {
        return Result.success(sysJobService.selectVoById(jobId, false));
    }

    @PreAuthorize("hasAuthority('monitor:job:add')")
    @PostMapping
    @Operation(summary = "新增定时任务")
    public Result add(@Validated(AddGroup.class) @RequestBody SysJobDTO dto) throws Exception {
        return Result.toAjax(sysJobService.insert(dto));
    }

    @PreAuthorize("hasAuthority('monitor:job:edit')")
    @PutMapping
    @Operation(summary = "修改定时任务")
    public Result edit(@Validated(EditGroup.class) @RequestBody SysJobDTO dto) throws Exception {
        return Result.toAjax(sysJobService.updateById(dto));
    }

    @PreAuthorize("hasAuthority('monitor:job:changeStatus')")
    @PutMapping("/changeStatus")
    @Operation(summary = "修改状态")
    @Log(title = "定时任务", businessType = BusinessType.UPDATE)
    public Result changeStatus(@Validated @RequestBody JobUpdateStatusDTO dto) throws Exception {
        return Result.toAjax(sysJobService.updateStatus(dto.getJobId(), dto.getStatus()));
    }

    @PreAuthorize("hasAuthority('monitor:job:remove')")
    @DeleteMapping("/{jobIds}")
    @Operation(summary = "删除定时任务")
    public Result remove(@PathVariable List<String> jobIds) throws Exception {
        for (String jobId : jobIds) {
            sysJobService.deleteById(jobId);
        }
        return Result.success();
    }

    @PreAuthorize("hasAuthority('monitor:job:changeStatus')")
    @PutMapping("/run/{jobId}")
    @Operation(summary = "立即执行一次")
    public Result run(@PathVariable String jobId) {
        return Result.success(sysJobService.run(jobId));
    }

    @PreAuthorize("hasAuthority('monitor:job:export')")
    @PostMapping("/export")
    @Operation(summary = "导出定时任务")
    @Log(title = "定时任务", businessType = BusinessType.EXPORT)
    public void export(HttpServletResponse response, SysJobParam param) {
        List<SysJob> list = sysJobService.selectList(param);
        ExcelUtil.exportExcel(list, SysJob.class, "定时任务", response);
    }
}
